package com.vigor.bear.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vigor.bear.mybatis.Pagination;
import com.vigor.bear.web.bean.Student;
import com.vigor.bear.web.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentController extends BaseController {

	@Autowired
	private StudentService ssv;

	@RequestMapping(method = RequestMethod.GET)
	public String view(Model model, HttpServletRequest request) {
		Pagination pagination = getPagination();

		List<Student> list = ssv.all(pagination, null);

		if (list != null && list.size() > 0) {
			model.addAttribute("records", list);
		}

		model.addAttribute("totalCounts", pagination.getTotal());
		model.addAttribute("pageSize", pagination.getSize());
		model.addAttribute("totalPages", pagination.getPages());
		model.addAttribute("student", new Student());

		// 填充select下拉条件,用来测试
		model.addAttribute("sexs", getSexType());
		model.addAttribute("academics", getAcademics());

		return "system/student/list";
	}

	@RequestMapping(value = "/list/{pageNow}/{pageSize}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(@PathVariable("pageNow") String pageNow,
			@PathVariable("pageSize") String pageSize, Student searchParams) {
		Pagination pagination = getPagination(pageNow, pageSize);
		List<Student> list = ssv.search(pagination, searchParams);

		Map<String, Object> map = new HashMap<String, Object>();
		if (list != null && list.size() > 0) {
			map.put("records", list);
		}
		map.put("pageSize", pagination.getSize());
		map.put("totalPages", pagination.getPages());
		map.put("totalCounts", pagination.getTotal());

		return map;
	}

	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> search(Model model, Student student,
			HttpServletRequest request) {
		Pagination pagination = getPagination();

		List<Student> list = ssv.search(pagination, student);

		Map<String, Object> map = new HashMap<String, Object>();
		if (list != null && list.size() > 0) {
			map.put("records", list);
		}

		map.put("pageSize", pagination.getSize());
		map.put("totalPages", pagination.getPages());
		map.put("totalCounts", pagination.getTotal());

		return map;
	}

	@RequestMapping(value = "/search2", method = RequestMethod.POST)
	public String search2(Student student, Model model) {
		Pagination pagination = getPagination();
		List<Student> list = ssv.search(pagination, student);

		if (list != null && list.size() > 0) {
			model.addAttribute("records", list);
		}

		model.addAttribute("totalCounts", pagination.getTotal());
		model.addAttribute("pageSize", pagination.getSize());
		model.addAttribute("totalPages", pagination.getPages());
		model.addAttribute("student", student);

		// 填充select下拉条件,用来测试
		model.addAttribute("sexs", getSexType());
		model.addAttribute("academics", getAcademics());

		return "system/student/list";
	}

	private static List<String> getSexType() {
		List<String> sexList = new ArrayList<String>();
		sexList.add("男");
		sexList.add("女");
		return sexList;
	}

	private static List<String> getAcademics() {
		List<String> academicsList = new ArrayList<String>();
		academicsList.add("信息工程");
		academicsList.add("光信息科学与技术(光通信)");
		academicsList.add("测控技术与仪器(信号处理与仪器)");
		academicsList.add("自动化");
		academicsList.add("通信工程(通信工程学院)");
		return academicsList;
	}

}
